package com.ihd.ihardware.view.tzc.health.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.R;
import com.ihd.ihardware.app.MyApplication;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.utils.DataUtils;
import com.ihd.ihardware.databinding.ActivityBindingBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.tzc.health.viewmodel.BindViewModel;
import com.inuker.bluetooth.library.beacon.Beacon;

import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.HexUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class BindingActivity extends BaseActivity<ActivityBindingBinding, BindViewModel> {
    private Animation rotate;
    private String originalData;
    private Disposable cheng;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();

        binding.cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin);
        if (rotate != null) {
            binding.rotateIV.startAnimation(rotate);
        } else {
            binding.rotateIV.setAnimation(rotate);
            binding.rotateIV.startAnimation(rotate);
        }
        binding.searchBtn.startWaveAnimation();

        setRx(AppConstans.BIND, new BaseConsumer<DeviceListRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(DeviceListRes mLiveData) {
                DialogUtils.successDialog(BindingActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "绑定成功");
                SPUtils.putString(AppConstans.DEVICES, new Gson().toJson(mLiveData.getData().getList(), new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
                }.getType()));
                Intent i = new Intent(BindingActivity.this, MeasureActivity.class);
                i.putExtra("familyId", MyApplication.memberId.getMemberId());
                i.putExtra("originalData", originalData);
                startActivity(i);
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(BindingActivity.this, error.getMessage());
            }
        });

        cheng = RxBus.getInstance().tObservable(AppConstans.BINDING, Beacon.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Beacon>() {
                    @Override
                    public void accept(Beacon beacon) throws Exception {
                        byte[] mByte = new byte[31];
                        System.arraycopy(beacon.mBytes, 0, mByte, 0, beacon.mBytes.length);
                        if (TextUtils.isEmpty(originalData)) {
                            HealthFragment.stopAdv();
                            binding.weightTV.setText(DataUtils.getWeight(mByte));
                            if (DataUtils.isStable(mByte)) {
                                originalData = HexUtils.bytesToHex(mByte);
                                viewModel.bind(originalData);
                            }
                        }
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_binding;
    }

    @Override
    protected Class getViewModelClass() {
        return BindViewModel.class;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.rotateIV.clearAnimation();
        if (cheng != null && !cheng.isDisposed())
            cheng.dispose();
    }
}
